<?php
add_action('widgets_init', 'register_submit_coupon_widget'); // load widget

function register_submit_coupon_widget() { // register widget
    register_widget('submit_coupon');
}

class submit_coupon extends WP_Widget {// widget class {

    function submit_coupon() {
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Submit coupon');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'submit_coupon');
        $this->WP_Widget('submit_coupon', 'CouponX - Submit coupon', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $post;
        $store_url = '';
        if($post->post_type == 'store'){
            $store_url = get_post_meta($post->ID,'url_store_metadata',true);
        }
        $title = apply_filters('widget_title', $instance['title']);
        $position = $instance['display_option'];
        $tax_event = get_terms('event', array('hide_empty' => false));
        ?>
        <script>
            jQuery(document).ready(function($) {
              $( "#ex_date" ).datepicker(
                  {dateFormat: 'yy/mm/dd'}
                );
            });
        </script>
        <div class="row-fluid submitcode">
            <div class="entry-header">
                 <h2 class="entry-title"><?php echo $title;?></h2>
            </div>
                    <div class="contac-quest">
                        <p>When you submit a coupon on <strong>MostCoupon</strong>, You help others save money.</p>
                        <div class="submit-success hidden">
                            <p>Thank!</p>
                            <p>The coupon will be posted after a quick review!</p>
                        </div>
                    </div>
                    <div class="row-fluid from-add-cp">
                        <form id="coupon_submit" name="form_submit"  method="post" enctype="multipart/form-data">
                            <div class="row-fluid store_url_field">
                                <?php if($position =='home'){ ?>
                                <label>Website store <span class="requir">*</span> :</label>
                               <?php }
                                     if($post && $post->post_type == 'store'){ ?>
                                    <input id="store_name" class="website" placeholder="only use url suggested by us"
                                           type="text" autocomplete="on" name="store_name" value="<?php echo get_post_meta($post->ID,'url_store_metadata',true);?>" readonly />
                                     <?php }else{?>
                                        <input id="store_name" class="website" placeholder="only use url suggested by us"
                                               type="text" autocomplete="off" name="store_name" />
                                     <?php }
                                  ?>
                                <div class="warning hiden domain-missing row-fluid">*Enter url store</div>
                                <div class="warning hiden domain-fail row-fluid">*Url store not match</div>
                            </div>
                            <div class="row-fluid title_field">
                                <?php if($position =='home'){ ?>
                                <label>Title Coupon<span class="requir">*</span> :</label>
                               <?php }?>
                                <input type="text" class="entertitle" type="text" placeholder="Product name and % discount" autocomplete="off" name="title_coupon" value="<?php echo $_POST['title_coupon'];?>" />
                                <div class="warning hiden title-missing">*Enter title coupon</div>
                            </div>
                            <div class="row-fluid coupon_type_field">
                                <?php if($position =='home'){ ?>
                                    <label>Offer type<span class="requir">*</span> :</label>
                               <?php }?>
                                <select id="select_server_played" name="select_type">
                                    <option value="code">Coupon Code</option>
                                    <option value="free_shipping">Freeshipping coupon</option>
                                    <option value="promotion">Promotion coupon</option>
                                </select>
                            </div>
                            <div class="row-fluid code_field">
                                <input type="text" class="entercode" type="text" placeholder="Coupon code"  autocomplete="off" name="coupon_code" value="<?php echo $_POST['coupon_code'];?>" />
                                <div class="warning hiden code-missing">*Enter coupon code</div>
                            </div>
                            <div class="row-fluid event_field">
                                <?php if($position =='home'){ ?>
                                    <label>Event:</label>
                               <?php }?>
                                    <select id="event_coupon" name="select_event">
                                        <option value="-1">Choose event (Optional)</option>
                                <?php 
                                    foreach($tax_event as $e){
                                        $e_name = $e->name;
                                        $e_slug = $e->slug;
                                ?>
                                        <option value="<?php echo $e_slug;?>"><?php echo $e_name;?></option>
                                    <?php }?>
                                    </select>
                            </div>
                            <div class="row-fluid des_field">
                                <?php if($position =='home'){ ?>
                                    <label>Describe the Discount<span class="requir">*</span> :</label>
                               <?php }?>
                                <textarea name="des_cp" placeholder="% discount, date of expiry, ongoing events, discounted product links, etc.."></textarea>
                                <div class="warning hiden des-missing">*Enter description</div>
                            </div>
                            <div class="row-fluid exdate_field">
                                <label>Expiration Date (optional) :</label>
                                <input class="date" type="text" placeholder="yy/mm/dd" autocomplete="off" name="ex_date" id="ex_date" value="<?php echo $_POST['ex_date'];?>"/>
                                <div class="warning hiden date-missing">*Enter Expiration Date</div>
                            </div>
                            <div class="row-fluid wrap-captcha security">
                                <img class="captcha_code" src="<?php echo get_template_directory_uri().'/inc/ajax-cp/cap/captcha_code_file.php'; ?>" alt="captcha"/>
                            </div>
                            <div class="row-fluid push_security">
                                <label>please type the characters below:<span class="requir">*</span> :</label>
                                <input class="word" id="security_code" placeholder="Security Code" autocomplete="off" name="6_letters_code" type="text" />
                                <div class="warning hiden captcha-missing">*Enter Security code</div>
                                <div class="warning hiden captcha-fail">*Security code not match</div>
                            </div>
                            <div class="row-fluid submit_coupon">
                                <div class="loading-submit-coupon hidden">
                                    <div id="floatingBarsG">
                                                <div class="blockG" id="rotateG_01">
                                                </div>
                                                <div class="blockG" id="rotateG_02">
                                                </div>
                                                <div class="blockG" id="rotateG_03">
                                                </div>
                                                <div class="blockG" id="rotateG_04">
                                                </div>
                                                <div class="blockG" id="rotateG_05">
                                                </div>
                                                <div class="blockG" id="rotateG_06">
                                                </div>
                                                <div class="blockG" id="rotateG_07">
                                                </div>
                                                <div class="blockG" id="rotateG_08">
                                                </div>
                                            </div>
                                </div>
                                <input id="submit_cp" type="submit" name="submit_coupon" value="Submit Code"/>
                            </div>
                        </form>
                    </div>
        </div>
        
   <?php }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['display_option'] = $new_instance['display_option'];
        return $instance;
    }

    function form($instance) {
        $arr_position = array('home' => 'Content Sidebar','right' => 'Right Sidebar');
        ?>
        <div style="color: #333;">
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'display_option' ); ?>"><?php _e('Position', ''); ?></label>
                <select name="<?php echo $this->get_field_name('display_option');?>" id="<?php echo $this-> get_field_id('display_option')?>" style="width: 60%;">
                    <?php 
                            foreach ($arr_position as $k=>$arr){
                                if($instance['display_option'] == $k)
                                    $selected = 'selected';
                                else
                                    $selected = ''; ?>
                                <option value='<?php echo $k;?>' <?php echo $selected;?>><?php echo $arr;?></option>    
                            <?php }
                    ?>
                </select>    
            </p>
        </div>
        <?php
    }
}
?>
